package entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.Objects;
import java.util.StringJoiner;

public class Order {
    private String id;  // 订单编号,客户编号,商品编号[],数量[],售价,成交日期
    private String customerId;  //客户编号
    private ArrayList<String> productsIds = new ArrayList<String>();  //商品编号
    private ArrayList<Integer> amounts = new ArrayList<Integer>();  //数量
    private double price;  //售价
    private Date transactionDate;  // 成交日期

    public Order(String id, String customerId, ArrayList<String> productsIds, ArrayList<Integer> amounts, double price, Date transactionDate) {
        this.id = id;
        this.customerId = customerId;
        if(productsIds!=null && amounts != null){
            this.productsIds = productsIds;
            this.amounts = amounts;
        }
        this.price = price;
        this.transactionDate = transactionDate;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    public ArrayList<String> getProductsIds() {
        return productsIds;
    }

    public void setProductsIds(ArrayList<String> productsIds) {
        this.productsIds = productsIds;
    }

    public ArrayList<Integer> getAmounts() {
        return amounts;
    }

    public void setAmounts(ArrayList<Integer> amounts) {
        this.amounts = amounts;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Date getTransactionDate() {
        return transactionDate;
    }

    public void setTransactionDate(Date transactionDate) {
        this.transactionDate = transactionDate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Order)) return false;
        Order order = (Order) o;
        return Double.compare(order.getPrice(), getPrice()) == 0 &&
                getId().equals(order.getId()) &&
                getCustomerId().equals(order.getCustomerId()) &&
                getProductsIds().equals(order.getProductsIds()) &&
                getAmounts().equals(order.getAmounts()) &&
                getTransactionDate().equals(order.getTransactionDate());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId(), getCustomerId(), getProductsIds(), getAmounts(), getPrice(), getTransactionDate());
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", Order.class.getSimpleName() + "[", "]")
                .add("id='" + id + "'")
                .add("customerId='" + customerId + "'")
                .add("productsIds=" + productsIds)
                .add("amounts=" + amounts)
                .add("price=" + price)
                .add("transactionDate=" + transactionDate)
                .toString();
    }
    public int getTotal(){
        int sum = 0;
        for (int amount : amounts){
            sum += amount;
        }
        return sum;
    }
}
